package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.common.AutoLog;
import com.example.common.Result;
import com.example.entity.*;
import com.example.entity.BusinessHomeInfo;
import com.example.service.TreeService;

import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tree")
public class TreeController {

    @Resource
    private TreeService treeService;

    //加载前端树苗检索页的树苗信息====================================================
    @PostMapping("/loadtree")
    public Result loadTree(@RequestBody Params params) {
        PageInfo<TreeAndTreeName> info = treeService.loadTree(params);
        return Result.success(info);
    }

    @PostMapping("/findtreebyname")
    public Result findTreeByName(@RequestBody String item) {
        Map<String, String> parse = (Map<String, String>) JSON.parse(item);
        List<Tree> list = treeService.findTreeByName(parse.get("treename"),parse.get("username"));
        return Result.success(list);
    }


    //通过检索信息筛选树苗信息显示在检索页中============================================
    @PostMapping("/search")
    public Result findTree(@RequestBody Params params) {
        PageInfo<TreeAndTreeName> info = treeService.findTree(params);
        return Result.success(info);
    }

    //通过检索信息筛选其他商家上架树苗信息显示在检索页中============================================
    @PostMapping("/findothertree/{userId}")
    public Result findOtherTree(@PathVariable Integer userId, @RequestBody Params params) {
        PageInfo<TreeAndTreeName> info = treeService.findOtherTree(userId, params);
        return Result.success(info);
    }

    //通过树苗类型筛选树苗信息显示在检索页中============================================
    @PostMapping("/findbytreetype")
    public Result findByTreeType(@RequestBody Params params) {
        PageInfo<TreeAndTreeName> info = treeService.findByTreeType(params);
        return Result.success(info);
    }

    //通过年份降序获取树苗信息===================================================
    @PostMapping("/findbyyeardesc")
    public Result findByYearDesc(@RequestBody Params params) {
        PageInfo<TreeAndTreeName> info = treeService.findByYearDesc(params);
        return Result.success(info);
    }

    //通过年份升序获取树苗信息
    @PostMapping("/findbyyearasc")
    public Result findByYearAsc(@RequestBody Params params) {
        PageInfo<TreeAndTreeName> info = treeService.findByYearAsc(params);
        return Result.success(info);
    }

    //通过树苗价格降序获取树苗信息
    @PostMapping("/findbypricedesc")
    public Result findByPriceDesc(@RequestBody Params params) {
        PageInfo<TreeAndTreeName> info = treeService.findByPriceDesc(params);
        return Result.success(info);
    }

    //通过树苗价格升序序获取树苗信息
    @PostMapping("/findbypriceasc")
    public Result findByPriceAsc(@RequestBody Params params) {
        PageInfo<TreeAndTreeName> info = treeService.findByYearAsc(params);
        return Result.success(info);
    }


    //获取前端首页树苗推荐的信息================================================
    @GetMapping("/findtreeinfo")
    public Result findTreeInfo() {
        List<Tree> treeList = treeService.findTreeInfo();
        return Result.success(treeList);
    }
    //获取商家在售树苗的信息================================================
    @PostMapping("/findbuinesstreeinfo/{userId}")
    public Result findBuinessTreeInfo(@PathVariable Integer userId) {
        List<Tree> treeList = treeService.findBuinessTreeInfo(userId);
        return Result.success(treeList);
    }




    @GetMapping("/findbyid")
    public Result findById(@RequestParam(value = "treeId") Integer treeId) {
        TreeAndTreeName treeList = treeService.findById(treeId);
        return Result.success(treeList);
    }

    @PostMapping("/treeedit")
    public Result treeEdit(@RequestBody TreeAndTreeName tree) {
        treeService.treeEdit(tree);
        return Result.success();
    }

    @DeleteMapping("/del/{treeId}")
    public Result delete(@PathVariable Integer treeId) {
        treeService.del(treeId);
        return Result.success();
    }

    //加载商家首页系统树苗数量==============================================
    @GetMapping("/loadtreenumber")
    public Result loadTreeNumber() {
        int treeNumber = treeService.loadTreeNumber();
        return Result.success(treeNumber);
    }

    //加载商家首页系统树苗种类==============================================
    @GetMapping("/loadtypenumber")
    public Result loadTypeNumber() {
        int typeNumber = treeService.loadTypeNumber();
        return Result.success(typeNumber);
    }

    //商家上架树苗
    @PostMapping("/addnewtree/{userId}")
    public Result addNewTree(@RequestBody TreeAndTreeName tree, @PathVariable Integer userId) {
        treeService.addNewTree(tree, userId);
        return Result.success();
    }

    //商家下架树苗
    @PostMapping("/deltree/{treeId}")
    public Result delTree(@PathVariable Integer treeId) {
        treeService.delTree(treeId);
        return Result.success();
    }

    //将树苗添加到收藏
    @PostMapping("/collect/{treeId}")
    public Result collectTree(@RequestBody User user, @PathVariable Integer treeId) {
        treeService.collectTree(user, treeId);
        return Result.success();
    }

    @PostMapping("/findcollect/{userId}")
    public Result findCollect(@RequestBody Params params, @PathVariable Integer userId) {
        PageInfo<CollectAndTreeTypeName> list = treeService.findCollect(params, userId);
        return Result.success(list);
    }

    @PostMapping("/businessfindcollect/{userId}")
    public Result businessFindCollect(@RequestBody Params params, @PathVariable Integer userId) {
        PageInfo<CollectAndTreeTypeName> list = treeService.businessFindCollect(params, userId);
        return Result.success(list);
    }

    @PostMapping("/cancelcollect/{collectId}")
    public Result cancelCollect(@PathVariable Integer collectId) {
        treeService.cancelCollect(collectId);
        return Result.success();
    }


    @PostMapping("/echarts/data/{username}")
    public Result loadData(@PathVariable("username") String username) {

        BusinessHomeInfo dto = new BusinessHomeInfo();

        List<Integer> systemtreelist = treeService.getSysData();
        List<Integer> usertreelist = treeService.getUserData(username);
        List<Integer> avgpricelist = treeService.getAvgPriceData();
        List<Integer> personpricelist = treeService.getPersonPriceData(username);

        dto.setSystemtreelist(systemtreelist);
        dto.setUsertreelist(usertreelist);
        dto.setAvgpricelist(avgpricelist);
        dto.setPersonalpricelist(personpricelist);

        return Result.success(dto);

    }
}
